package com.test.test.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.test.test.dao.UserRepos;
import com.test.test.dto.common.PageParam;
import com.test.test.dto.common.PageResp;
import com.test.test.dto.common.RespConsts;
import com.test.test.dto.common.ResultResp;
import com.test.test.dto.userModule.RolePamar;
import com.test.test.dto.userModule.UserParam;
import com.test.test.dto.userModule.UserResp;
import com.test.test.entity.User;
import com.test.test.service.RoleService;
import com.test.test.service.UserService;
import com.test.test.utils.CheckParameterUtil;
import com.test.test.utils.CoreUtils;
import com.test.test.utils.ExceptionUtil;
import com.test.test.utils.ResultRespUtil;
import com.test.test.utils.aspect.AdminOnly;
import com.test.test.utils.aspect.NeedSecured;
import com.test.test.utils.emums.State;

//@Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
@Transactional(rollbackOn = Exception.class)
@Service
@NeedSecured
public class UserServiceImpl implements UserService {

	@Autowired
	UserRepos userRepos;
	@Autowired
	RoleService roleService;

	@Override
	public ResultResp<Integer> addUser(UserParam userDto) throws Exception {
		// TODO Auto-generated method stub
		ResultResp<Integer> resp = new ResultResp<Integer>();
		CheckParameterUtil.checkIsNullOfStudy("参数不能为空", userDto);
		CheckParameterUtil.checkIsEmptyOfStudy("邮箱不能为空", userDto.getEmail());
		CheckParameterUtil.checkIsEmptyOfStudy("手机不能为空", userDto.getMobile());
		User user = new User();
		BeanUtils.copyProperties(userDto, user);
		userRepos.save(user);
		resp.setData(user.getId());
		ResultRespUtil.successCode(resp);
		return resp;
	}

	@Override
	public ResultResp<Boolean> modifyUser(UserParam userDto) throws Exception {
		// TODO Auto-generated method stub
		ResultResp<Boolean> resp = new ResultResp<Boolean>();
		User user = validUserPamar(userDto);
		// BeanUtils.copyProperties(userDto, user);
		user.setMobile(userDto.getMobile());
		user.setValidity(userDto.getValidity());
		userRepos.save(user);
		resp.setData(true);
		ResultRespUtil.successCode(resp);
		return resp;
	}

	// 验证数据
	private User validUserPamar(UserParam userDto) {
		CheckParameterUtil.checkIsNullOfStudy("参数不能为空", userDto);
		Integer id = userDto.getId();
		CheckParameterUtil.checkIsNullOfStudy("userId不能为空", id);
		User user = findByUserId(id);
		return user;
	}

	private User findByUserId(Integer id) {
		User user = userRepos.findById(id);
		if (CoreUtils.isNull(user)) {
			throw ExceptionUtil.newDataException(RespConsts.OPERATE_DATA_NOT_EXIST,
					RespConsts.getPrompt(RespConsts.OPERATE_DATA_NOT_EXIST));
		}
		return user;
	}

	@Override
	public ResultResp<Boolean> modifyPassword(UserParam userDto) throws Exception {
		// TODO Auto-generated method stub
		ResultResp<Boolean> resp = new ResultResp<Boolean>();
		User user = validUserPamar(userDto);
		user.setPassword(userDto.getPassword());
		userRepos.save(user);
		resp.setData(true);
		ResultRespUtil.successCode(resp);
		return resp;
	}

	@Override
	public ResultResp<List<UserResp>> userList(UserParam param) throws Exception {
		// TODO Auto-generated method stub
		ResultResp<List<UserResp>> resp = new ResultResp<List<UserResp>>();
		List<UserResp> respList = new ArrayList<UserResp>();
		List<User> userList = userRepos.findByUserName(param.getUserName());
		for (User user : userList) {
			UserResp userResp = new UserResp();
			BeanUtils.copyProperties(user, userResp);
			respList.add(userResp);
		}
		resp.setData(respList);
		ResultRespUtil.successCode(resp);
		return resp;
	}

	@Override
	public ResultResp<PageResp<UserResp>> pageUser(PageParam<UserParam> param) throws Exception {
		// TODO Auto-generated method stub
		ResultResp<PageResp<UserResp>> resp = new ResultResp<>();
		Sort sort = new Sort(Direction.DESC, "id");
		Integer pageNum = param.getPageNum();
		Integer pageSize = param.getPageSize();
		Pageable pageable = new PageRequest(pageNum -  1, pageSize, sort);
		Page<User> pageUser = userRepos.findAll(pageable);
		UserParam args = param.getArgs();
		Page<User> findByUserName = userRepos.findByUserName(args.getUserName(), pageable);
		List<User> content = findByUserName.getContent();
		List<UserResp> userRespList = new ArrayList<UserResp>();
		for (User user : content) {
			UserResp userReps = new UserResp();
			BeanUtils.copyProperties(user, userReps);
			userRespList.add(userReps);
		}
		PageResp<UserResp> userPageResp = new PageResp<>();
		userPageResp.setList(userRespList);
		userPageResp.setTotalcount(pageUser.getTotalPages());
		userPageResp.setPageNum(pageNum);
		userPageResp.setPageSize(pageSize);
		resp.setData(userPageResp);
		ResultRespUtil.successCode(resp);
		return resp;
	}

	@Override
	public ResultResp<UserResp> findById(Integer userId) throws Exception {
		// TODO Auto-generated method stub
		CheckParameterUtil.checkIsNullOfStudy("userId不能为空", userId);
		ResultResp<UserResp> resp = new ResultResp<>();
		UserResp userResp = new UserResp();
		User user = findByUserId(userId);
		BeanUtils.copyProperties(user, userResp);
		resp.setData(userResp);
		ResultRespUtil.successCode(resp);
		return resp;
	}

	@Override
	public ResultResp<Boolean> deleteUser(Integer userId) throws Exception {
		// TODO Auto-generated method stub
		CheckParameterUtil.checkIsNullOfStudy("userId不能为空", userId);
		ResultResp<Boolean> resp = new ResultResp<>();
		User user = findByUserId(userId);
		userRepos.delete(user);
		resp.setData(true);
		ResultRespUtil.successCode(resp);
		return resp;
	}

	@Override
	public ResultResp<UserResp> checkedLogin(UserParam userDto) throws Exception {
		// TODO Auto-generated method stub
		CheckParameterUtil.checkIsNullOfStudy("参数不能为空", userDto);
		ResultResp<UserResp> resp = new ResultResp<>();
		String mobile = userDto.getMobile();
		String password = userDto.getPassword();
		CheckParameterUtil.checkIsEmptyOfStudy("帐号不能为空", mobile);
		CheckParameterUtil.checkIsEmptyOfStudy("密码不能为空", password);
		User user = userRepos.findByMobileAndPassword(mobile, password);
		if (CoreUtils.isNull(user)) {
			throw ExceptionUtil.newDataException(RespConsts.OPERATE_DATA_NOT_EXIST,
					RespConsts.getPrompt(RespConsts.OPERATE_DATA_NOT_EXIST));
		}
		UserResp userResp = new UserResp();
		BeanUtils.copyProperties(user, userResp);
		resp.setData(userResp);
		ResultRespUtil.successCode(resp);
		return resp;
	}

	@Override
	@AdminOnly
	public ResultResp<Boolean> tetsRollBack() throws Exception {
		// TODO Auto-generated method stub
		ResultResp<Boolean> resp = new ResultResp<Boolean>();
		UserParam userDto = new UserParam();
		userDto.setUserName("测试");
		userDto.setMobile("1383434");
		userDto.setEmail("1212");
		ResultResp<Integer> addUser = addUser(userDto);
		RolePamar pamar = new RolePamar();
		pamar.setRoleName("测试测试");
		pamar.setState(State.ENABLE.getCode());
		ResultResp<Integer> addRole = roleService.addRole(pamar);
		resp.setData(true);
		
		ResultRespUtil.successCode(resp);
//		if(true)	
//		throw ExceptionUtil.newDataException(RespConsts.OPERATE_DATA_NOT_EXIST,
//				RespConsts.getPrompt(RespConsts.OPERATE_DATA_NOT_EXIST)); 
		return resp;
	}

}
